/*
 * Copyright (C) 2003, Velocityme Partnership
 * SearchEntry.java
 *
 * Created on 15 November 2004, 09:03
 */

package com.velocityme.utility;

import com.velocityme.entity.ChangeDelta;
import com.velocityme.entity.FileAttachment;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

/**
 *
 * @author  Robert
 */
public class SearchEntry implements Serializable {
    
    private Long m_taskId;
    private Integer m_priority;
    private Collection<Transaction> m_transactions;
    
    public class Transaction implements Serializable {
        private String m_type;
        private Date m_date;
        private String m_person;
        private String m_user;
        private FileAttachment m_fileAttachment;
        private Collection<Row> m_rows;
        
        public class Row implements Serializable {
            private String m_name;
            private String m_value;
            
            public Row(String name, String value) {
                m_name = name;
                m_value = value;
            }
            public String getName() { return m_name; }
            public String getValue() { return m_value; }
        }
        
        public Transaction(String type, Date date, String person, String user, FileAttachment fileAttachment) {
            m_type = type;
            m_date = date;
	    m_person = person;
	    m_user = user;
            m_fileAttachment = fileAttachment;
            m_rows = new ArrayList<Row>();
       }
        
        public void addRow(String name, String value) {
            m_rows.add(new Row(name, value));
        }
        
        public String getType() { return m_type; }
        public Date getDate() { return m_date; }
        public String getPerson() { return m_person; }
        public String getUser() { return m_user; }
        public FileAttachment getFileAttachment() { return m_fileAttachment; }
        public Collection<Row> getRows() { return m_rows; }
    }
    
    /** Creates a new instance of SearchEntry */
    public SearchEntry(Long taskId, Integer priority) {
        m_taskId = taskId;
        m_priority = priority;
        m_transactions = new ArrayList<Transaction>();
    }
    
    public Transaction addTransaction(String type, ChangeDelta cdLocal) {
        FileAttachment fileAttachment = cdLocal.getFileAttachment();
        Transaction transaction = new Transaction(type, 
                                                  cdLocal.getCreationTime(),
                                                  cdLocal.getUser().getPerson().getName(),
                                                  cdLocal.getUser().getName(),
                                                  fileAttachment);
        m_transactions.add(transaction);
        return transaction;
    }
    
    public Long getTaskId() { return m_taskId; }
    
    public Integer getPriority() { return m_priority; }
    
    public Collection<Transaction> getTransactions() { return m_transactions; }
    
    public int getNumTransactions() { return m_transactions.size(); }
    
}
